#!coding:utf-8
# vim: tabstop=4 shiftwidth=4 softtabstop=4
"""
    @author join.kang
"""
from rest_framework.response import Response
from cvirt.manager.models import Proposal, Network
from cvirt.manager.serializers import ProposalSerializer

from cvirt.manager.views import BaseListAPIView, NamedAPIViewMixIn, BaseRetriveAPIView


class ProposalList(NamedAPIViewMixIn, BaseListAPIView):
    model = Proposal
    serializer_class = ProposalSerializer

    def _get(self, request, *args, **kwargs):
        dataset = Proposal.objects.all()
        result = self.paginate(request,dataset,ProposalSerializer)
        return Response(result)

    # def add(self, request, *args, **kwargs):
    #     name = request.DATA.get('name',None)
    #     memory = request.DATA.get('memory',0)
    #     disk = request.DATA.get('disk',0)
    #     cpu = request.DATA.get('cpu',0)
    #     networkid = request.DATA.get('network',None)
    #     network = Network.objects.get(pk=networkid) if networkid else None
    #     interface_type = request.DATA.get('interface_type','e1000')
    #     desc = request.DATA.get('desc',None)
    #
    #     Proposal.objects.create(name=name,memory=memory,disk=disk,cpu=cpu,network=network,interface_type=interface_type,desc=desc)
    #     return Response({"result":"ok"})

    def delete(self, request, *args, **kwargs):
        ids = request.DATA.get('ids')
        for id in ids:
            Proposal.objects.filter(id=id).delete()
        return Response(status=200)


class ProposalInfo(NamedAPIViewMixIn,BaseListAPIView):
    model = Proposal
    serializer_class = ProposalSerializer

    def update(self, request, *args, **kwargs):
        #TODO:USE REST SERIALIZER CLASS TO BUILD A MODEL
        #ps = ProposalSerializer.deserialize('json',request.DATA)
        request_data = request.DATA
        network = Network.objects.get(name=request_data.get('network').get('name'))
        Proposal.objects.filter(id=request_data.get('id')).update(name=request_data.get('name'),memory=int(request_data.get('memory')),
            disk=int(request_data.get('disk')),cpu=int(request_data.get('cpu')),network=network,interface_type=request_data.get('interface_type'))
        return Response(200)
